package space.edge.d.demo.qrcode.privatecode;


import space.edge.d.demo.qrcode.common.BarcodeFormat;
import space.edge.d.demo.qrcode.common.BinaryBitmap;
import space.edge.d.demo.qrcode.common.DecodeHintType;
import space.edge.d.demo.qrcode.common.DecoderResult;
import space.edge.d.demo.qrcode.common.Reader;
import space.edge.d.demo.qrcode.common.Result;
import space.edge.d.demo.qrcode.common.ResultMetadataType;
import space.edge.d.demo.qrcode.privatecode.decoder.Decoder;
import space.edge.d.demo.qrcode.privatecode.detector.Detector;
import space.edge.d.demo.qrcode.privatecode.detector.DetectorResult;
import space.edge.d.demo.qrcode.exception.ChecksumException;
import space.edge.d.demo.qrcode.exception.FormatException;
import space.edge.d.demo.qrcode.exception.NotFoundException;

import java.util.Map;

/**
 * Created by gsl on 2017/10/16.
 */

public class CodeReader implements Reader {

    private Decoder decode = new Decoder();

    @Override
    public Result decode(BinaryBitmap image) throws NotFoundException, FormatException, ChecksumException {
        return decode(image, null);
    }

    @Override
    public Result decode(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException, FormatException, ChecksumException {
        // TODO: 2017/10/16 一个纯码图片
        DetectorResult detectorResult = new Detector(image.getBlackMatrix()).detect(hints);
        DecoderResult decoderResult = decode.decode(detectorResult.getBits(), hints);
        Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), detectorResult.getPoints(), BarcodeFormat.PRIVATE_CODE);
        String ecLevel = decoderResult.getECLevel();
        if (ecLevel != null) {
            result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, ecLevel);
        }
        return result;
    }

    @Override
    public void reset() {

    }

}
